﻿package quiero
{
	
	import flash.events.*;
	import flash.net.*;
	import flash.display.Loader
	import flash.media.Sound
	public class Quiero
	{
		private static var _params:Array=["url"];
		private static var _optionalParams:Array=["onComplete","format","method","toVars","onProgress","onIOError","onSecurityError","sound","loader","file"];
		private static var _requests:Array = new Array();
		private static var _count:uint=0;
		public static var types:Class=URLLoaderDataFormat;
		public static var methods:Class=URLRequestMethod;
		public static function request(o:Object,load:Boolean=true):URLRequest
		{
			var _url:String=o.url;
			var _data:Object=o.data;
			var _type:String=o.type;
			var _method:String=o.method;
			var _onComplete:Function=o.onComplete;
			
			for (var i:uint=0; i<_params.length; i++)
			{
				if (o[_params[i]]==null)
				{
					throw new Error("[Quiero] Expected property "+_params[i]+" on params object!");
				}
			}
			if (o.method!=null)
			{
				if (String(o.method).toUpperCase()=="POST")
				{
					o.method=URLRequestMethod.POST;
				}
				if (String(o.method).toUpperCase()=="GET")
				{
					o.method=URLRequestMethod.GET;
				}
			}

			var req:Requester=new Requester(_count,_url,_method,_type,o.file,o.loader,o.sound);
			var listenTo:EventDispatcher
			if(o.file != null){
				listenTo = o.file
			}else if(o.loader != null)
			{
				listenTo = o.loader
			}else if(o.sound != null){
				listenTo = o.sound
			}else{
				listenTo = req.loader
			}
			if (o.forceRefresh)
			{
				try
				{
					o.data[Random.randomString()]=Random.randomString();
				}
				catch (e:TypeError)
				{
					trace("[Quiero] You should turn off 'forceRefresh'. It doesn't work with the data you specified.");
				} catch(e:ReferenceError){
					var u:URLVariables = new URLVariables();
					u.data[Random.randomString()]=Random.randomString();
					o.data = u
				}
			}
			if (o.toVars==null)
			{
				o.toVars=true;
			}
			if (o.constructor==Object)
			{
				o.data=objToVars(o.data);
			}
			req.data=o.data;
			_requests.push(req);
			if (o.method is String)
			{
				req.request.method=o.method;
			} else
			{
				req.request.method=URLRequestMethod.POST;
			}
			if (o.type is String)
			{
				o.format=o.type;
			}
			if(listenTo is URLLoader){
			if (o.format is String)
			{
				URLLoader(listenTo).dataFormat=o.format;
			} else
			{
				URLLoader(listenTo).dataFormat=URLLoaderDataFormat.TEXT;
			}
			}
			if (o.headers is Array)
			{
				req.request.requestHeaders=o.header;
			}
			if (o.onProgress is Function)
			{
				listenTo.addEventListener(ProgressEvent.PROGRESS,o.onProgress as Function);
			}
			if (o.onIOError is Function)
			{
				listenTo.addEventListener(IOErrorEvent.IO_ERROR,o.onIOError as Function);
			}
			if (o.onSecurityError is Function)
			{
				listenTo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,o.onProgress as Function);
			}
			if (o.onComplete is Function)
			{
				req.addEventListener(Event.COMPLETE,_onComplete);
			}
			if (load)
			{
				req.load();
			}
			_requests.push(req);
			_count++;
			return req.request;
		}


		private static function objToVars(o:Object):URLVariables
		{
			var u:URLVariables = new URLVariables();
			for (var key:String in o)
			{
				u[key]=o[key];
			}
			return u;
		}



	}

}

class Random
{
	public static var allowed:String="abcdefghijklmnopqrstuvwxyz0123456789";
	public static var last:String="";
	public static function randomString(length:uint=10):String
	{
		do
		{
			var t:String="";
			for (var i=0; i<length; i++)
			{
				t+=allowed.charAt(randomInt(0,allowed.length-1));
			}
		} while (t == last);
		last=t;
		return t;
	}

	public static function randomInt(min:int,max:int):int
	{
		return Math.round((Math.random()*max)+min);
	}
}

import flash.events.*;
import flash.net.*;
import flash.display.*
import flash.media.Sound
import quiero.*;
class Requester extends EventDispatcher
{
	private var _busy:Boolean=true;
	public var request:URLRequest;
	public var flLoader:Loader
	private var _loader:URLLoader;
	public var vars:URLVariables;
	public var file:FileReference
	private var _id:uint;
	private var listen:EventDispatcher
	private var sound:Sound
	public function Requester(id:uint,url:String,method:String="GET",type:String="application/x-www-form-urlencoded",file:FileReference=null,loader2:Loader=null,sound2:Sound=null)
	{
		if (method==null||method=="")
		{
			method="GET";
		}
		if (type==null||type=="")
		{
			type="application/x-www-form-urlencoded";
		}
		this._id=id;
		request=new URLRequest(url);
		_loader = new URLLoader()
		_loader.dataFormat=type;
		request.method=method;
		request.data=vars;		
		if(file != null){
			this.file = file
			listen=file
		}else if(loader2 != null)
		{
			this.flLoader = loader2
			listen = loader2
		}else if(sound2 != null){
			this.sound =sound2
			listen =sound
		}else{
			listen = _loader
		}
	}
	
	public function set data(o:Object):void
	{
		request.data = o
	}
		


	public function load():void
	{
		if(listen is Loader){
			Loader(listen).contentLoaderInfo.addEventListener(Event.COMPLETE,update)
		}else{
			listen.addEventListener(Event.COMPLETE,update);
		}
		if(listen is URLLoader){
			URLLoader(listen).load(request);
		}else if(listen is FileReference){
			FileReference(listen).upload(request)
		}else if(listen is Sound){
			Sound(listen).load(request)
			Sound(listen).play()
		}else if(listen is Loader){
			Loader(listen).load(request)
		}
	}

	private function update(e:Event):void
	{
		dispatchEvent(new RequestEvent(Event.COMPLETE,_loader.data));

	}



	public function get loader():URLLoader
	{
		return _loader;
	}
	

}